package aStar;

import java.util.ArrayList;
import javax.swing.JFrame;


public class Tester extends	JFrame {
		
	private static final long serialVersionUID = 1L;
	
	public static final int FIELD_WIDTH = 50;
	public static final int FIELD_HEIGHT = 30;
	private static final int MOUNTAIN = FIELD_WIDTH*FIELD_HEIGHT/3;
	private static final int DISABLED_TILES = (int)(FIELD_WIDTH * FIELD_HEIGHT * .0);

	private static final int SIMULATIONS = 5;
	private static ArrayList<ArrayList<Tile>> path = new ArrayList<ArrayList<Tile>>();
	
	public static AStar pathfinder;		

	public static void main( String args[] ) {
			
		//make a test field and generate some mountains
		Field data = new Field();
		for(int i=0; i<MOUNTAIN ;i++){
			data.elevateTiles((int)(Math.random()*FIELD_WIDTH), (int)(Math.random()*FIELD_HEIGHT), (int)(Math.random()*2), (int)(Math.random()*6));
		}
		
		//Disable some tiles
		for(int i=0; i<DISABLED_TILES;i++) {
			data.getTile((int)(Math.random()*FIELD_WIDTH), (int)(Math.random()*FIELD_HEIGHT)).setWalkable(false);
		}
		
		//init of a new pathfinder
		pathfinder = new AStar();
		
		//set some stuff for the pathfinder
		pathfinder.setField(data);
		
		//look for a path from origin to destination previusly set
		long time = 0;
		
		for (int i = 0 ; i < SIMULATIONS ; i++) {
			data.cleanField();
			pathfinder.setOrigin(data.getTile((int)(Math.random()*FIELD_WIDTH), (int)(Math.random()*FIELD_HEIGHT)));
			pathfinder.setDestination(data.getTile((int)(Math.random()*FIELD_WIDTH), (int)(Math.random()*FIELD_HEIGHT)));
			System.out.println("Starting pathfinder for ineration " + i);
			path.add(pathfinder.findPath());
			time += pathfinder.getLastTime();
		}
		
		System.out.println();
		System.out.println("Ran "+SIMULATIONS+" simulations");
		System.out.println("Total IN Simulation time:" + time+" ms");
		System.out.println("Average simulation time "+(time/SIMULATIONS)+" ms");
		
		//Make an frame to visualize the found data
		JFrame mainFrame	= new AStarGUI(data, path);
	//	ToolTip scanner = new ToolTip((AStarGUI) mainFrame);
		
//		mainFrame.addMouseListener(scanner);
  //      mainFrame.addMouseMotionListener(scanner);
        
		mainFrame.setSize(1280,800);
		mainFrame.setVisible( true );
		mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		// data.exportField();
	}

	public static void sleep(int sleep) throws InterruptedException {
		Thread.sleep(sleep);
		
	}
}